/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date: 2025-03-24
 * Time: 9:42
 */
public class Main {
    // 插入排序
    public static void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = 0;
            for (j = i - 1; j >= 0; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                } else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }


    // 希尔排序
    public static void shellSort(int[] array) {
        int gap = array.length / 2;
        while (gap > 0) {
            shell(array, gap);
            gap /= 2;
        }
    }

    private static void shell(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (array[j] > tmp) {
                    array[j + gap] = array[j];
                } else {
                    break;
                }
            }
            array[j + gap] = tmp;
        }
    }

    // 选择排序
    public static void selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int min = i;
            for (int j = i; j < array.length; j++) {
                if (array[j] < array[min]) {
                    min = j;
                }
            }
            if (min != i) {
                int tmp = array[min];
                array[min] = array[i];
                array[i] = tmp;
            }
        }
    }

    public static void selectSort2(int[] array) {
        int left = 0;
        int right = array.length - 1;

        while (left < right) {
            int max = right;
            int min = left;
            for (int i = min; i < max; i++) {
                if (array[i] < array[min]) {
                    min = i;
                }
                if (array[i] > array[max]) {
                    max = i;
                }
            }
            swap(array, min, left);
            if (left == max) {
                max = min;
            }
            swap(array, max, right);
            left++;
            right--;

        }
    }

    public static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    // 堆排序
    public static void heapSort(int[] array) {
        createHeap(array);
        int end = array.length - 1;
        while (end > 0) {
            swap(array, 0, end);
            siftDown(array, 0, end);
            end--;
        }
    }

    public static void createHeap(int[] array) {
        for (int parent = (array.length - 1 - 1) / 2; parent >= 0; parent--) {
            siftDown(array, parent, array.length);
        }
    }

    private static void siftDown(int[] array, int parent, int length) {
        int child = 2 * parent + 1;
        while (child < length) {
            if (child + 1 < length && array[child] < array[child + 1]) {
                child++;
            }
            if (array[child] > array[parent]) {
                swap(array, child, parent);
                parent = child;
                child = 2 * parent + 1;
            } else {
                break;
            }
        }
    }

    // 冒泡排序
    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] > array[j + 1]) {
                    swap(array, j, j + 1);
                    flg = true;
                }
            }
            if (!flg) {
                break;
            }
        }
    }

    // 快速
    public static void quickSort(int[] array) {
        quick(array, 0, array.length - 1);
    }

    private static void quick(int[] array, int start, int end) {
        if (start >= end) {
            return;
        }

        //三数取中
        int index = midNum(array, start, end);

        int par = partition(array, start, end);

        quick(array, start, par - 1);

        quick(array, par + 1, end);
    }

    private static int midNum(int[] array, int left, int right) {
        int mid = (left + right) / 2;
        if (array[left] < array[right]) {
            if (array[mid] > array[left]) {
                return left;
            } else if (array[mid] > array[right]) {
                return right;
            } else {
                return mid;
            }
        } else {
            if (array[mid] > array[left]) {
                return left;
            } else if (array[mid] < array[right]) {
                return right;
            } else {
                return mid;
            }
        }
    }
    private static int partition(int[] array,int left,int right){
        int i = left;
        int tmp = array[left];
        while (left < right){
            while (left < right && array[right] >= tmp){
                right--;
            }
            while (left < right && array[left] <= tmp){
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }


    // 归并排序---递归
    public static void mergeSort(int[] array){
        mergeSortChild(array,0,array.length-1);
    }
    private static void mergeSortChild(int[] array,int left,int right){
        if (left >= right){
            return;
        }

        int mid = (left+right)/2;

        mergeSortChild(array, left, mid);
        mergeSortChild(array,mid,right);

        merge(array,left,mid,right);
    }
    private static void merge(int[] array,int left,int mid,int right){
        int[] tmpArr = new int[right-left+1];
        int k = 0;//tmpArr数组下标

        int s1 = left;
        int e1 = mid;
        int s2 = mid + 1;
        int e2 = right;
        while (s1 <= e1 && s2 <= e2){
            if (array[s1] < array[s2]){
                tmpArr[k] = array[s2];
                k++;
                s1++;
            }else {
                tmpArr[k] = array[s2];
                k++;
                s2++;
            }
        }
        while (s2 <= e1){
            tmpArr[k++] = array[s1++];
        }
        while (s1 <= e1){
            tmpArr[k++] = array[s1++];
        }
        for (int i = 0; i < k; i++) {
            array[i+left] = tmpArr[i];
        }
    }

// 归并排序---非递归
    public static void mergeSortNor(int[] array){
        int gap = 1;
        while (gap < array.length){
            for (int i = 0; i < array.length; i++) {
                int left = i;
                int mid = left+gap-1;
                if (mid >= array.length){
                    mid = array.length - 1;
                }
                int right = mid + gap;
                if (right >= array.length){
                    right = array.length;
                }
                merge(array,left,mid,right);
            }
            gap *= 2;
        }
    }

// 计数排序
    public static void countSort(int[] array){
        //1.求最大值和最小值
        int max = array[0];
        int min = array[0];
        for (int i = 0; i < array.length; i++) {
            if (array[i] < min){
                min = array[i];
            }
            if (array[i] > max){
                max = array[i];
            }
        }
        int len = max - min + 1;
        int[] countArray = new int[len];
        for (int i = 0; i < array.length; i++) {
            int index = array[i] - min;
            countArray[index]++;
        }
        int k = 0;
        for (int i = 0; i < countArray.length; i++) {
            while (countArray[i] != 0){
                array[k] = i + min;
                k++;
                countArray[i]--;
            }
        }
    }
}
